package com.wtwd.device.push;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.push.model.v20160801.*;
import com.google.gson.Gson;
import com.wtwd.device.constant.ClientTypeEnum;
import com.wtwd.device.constant.PushErrorEnum;
import com.wtwd.device.model.vo.ResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author mjy
 * @date 2021/10/28
 * @description
 */
public class AliyunMobilePushUtil {

    private static final Logger logger = LoggerFactory.getLogger(AliyunMobilePushUtil.class);


    protected static final String accessKeyId = "LTAI4GGQfzjPo2xGAbC8PsT7";
    protected static final String accessKeySecret = "XwFktlMIUhKHTnMhVGcpgAI5HDsrvp";
    protected static final String region = "cn-hangzhou";

    protected static DefaultAcsClient client;


    /**
     * 初始化客户端数据
     */
    private static void initClient() {
        logger.info("=====AliyunMobilePushUtilInitClient=======");
        IClientProfile profile = DefaultProfile.getProfile(region, accessKeyId, accessKeySecret);
        if (client == null) {
            client = new DefaultAcsClient(profile);
        }
    }


    /**
     * 查询设备详情
     *
     * @param appKey
     * @param deviceId
     * @return
     */
    public static QueryDeviceInfoResponse.DeviceInfo queryDeviceInfo(long appKey, String deviceId) {
        initClient();
        QueryDeviceInfoRequest request = new QueryDeviceInfoRequest();
        request.setAppKey(appKey);
        request.setDeviceId(deviceId);
        QueryDeviceInfoResponse.DeviceInfo deviceInfo = null;
        try {
            QueryDeviceInfoResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
            deviceInfo = response.getDeviceInfo();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        } finally {
            return deviceInfo;
        }
    }


    /**
     * 调用CheckDevices批量验证设备有效性
     *
     * @param appKey
     * @param deviceIds
     */
    public static void testCheckDevices(long appKey, String deviceIds) {
        initClient();
        CheckDevicesRequest request = new CheckDevicesRequest();
        request.setAppKey(appKey);
        request.setDeviceIds(deviceIds);
        CheckDevicesResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        System.out.println(JSON.toJSONString(response));
    }

    /**
     * 推送
     *
     * @param pushEntity
     * @return
     */
    public static ResultVO pushApp(PushEntity pushEntity) {
        logger.info("pushApp param=" + pushEntity.toString());
        initClient();
        PushErrorEnum pushErrorEnum = null;
        PushResponse acsResponse = null;
        try {
            PushRequest pushRequest = transTo(pushEntity);
            acsResponse = client.getAcsResponse(pushRequest);
        } catch (ClientException e) {
            logger.info("推送app 异常", e.getRequestId());
            switch (e.getErrCode()) {
                case "Missing{ParameterName}":
                    pushErrorEnum = PushErrorEnum.PARAM_MISS;
                    break;
                case "AccountProhibited":
                    pushErrorEnum = PushErrorEnum.ACCOUNT_PROHIBITED;
                    break;
                case "AccountInDebt":
                    pushErrorEnum = PushErrorEnum.ACCOUNT_INDEBT;
                    break;
                case "NoPermission":
                    pushErrorEnum = PushErrorEnum.NO_PERMISSION;
                    break;
                case  "PermissionDenied":pushErrorEnum =PushErrorEnum.PERMISSION_DENIED;
                break;
                default:
                    pushErrorEnum = PushErrorEnum.FAIL;

            }
            return ResultVO.error(pushErrorEnum);
        }
        PushVo pushVo = new PushVo();
        pushVo.setMessageId(acsResponse.getMessageId());
        pushVo.setRequestId(acsResponse.getRequestId());
        return ResultVO.success(pushVo);
    }


    private static PushRequest transTo(PushEntity pushEntity) {//暂时只转换必填参数
        if (pushEntity == null) {
            return null;
        }
        PushRequest pushRequest = new PushRequest();
        pushRequest.setAppKey(pushEntity.getAppKey());
        pushRequest.setDeviceType(pushEntity.getDeviceType());
        pushRequest.setPushType(pushEntity.getPushType());
        pushRequest.setTarget(pushEntity.getTarget());
        pushRequest.setTargetValue(pushEntity.getTargetValue());
        pushRequest.setTitle(pushEntity.getTitle());
        pushRequest.setBody(pushEntity.getBody());
        if (ClientTypeEnum.ANDROID.getValue().equals(pushRequest.getDeviceType())) {
            pushRequest.setAndroidExtParameters(pushEntity.getExtraParams());
        } else {
            pushRequest.setIOSExtParameters(pushEntity.getExtraParams());
            pushRequest.setIOSApnsEnv("DEV");
        }
        return pushRequest;
    }


}
